﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 人事工资管理系统
{
    public partial class frmEmployeeInfo : Form
    {
        public frmEmployeeInfo()
        {
            InitializeComponent();
        }
        public string YGName;
        public string YGID;
        PMSClass.DBOperate operate = new PMSClass.DBOperate();
        OpenFileDialog OpenFileDialog1 = new OpenFileDialog();
        private void frmEmployeeInfo_Load(object sender, EventArgs e)
        {
            operate.BindDropdownlist("tb_department", cbbYGBumen, 1);
            operate.BindDropdownlist("tb_userJob", txtYGZhiwu, 1);
            this.Text = "[" + YGName + "]的个人信息";
            string sql = "select * from tb_employee where employeeID='" + YGID + "'";
            DataSet ds = operate.GetTable(sql);
            ds.Dispose();
            txtYGNum.Text = ds.Tables[0].Rows[0][1].ToString();
            txtYGName.Text = ds.Tables[0].Rows[0][2].ToString();
            cbbYGSex.SelectedItem = ds.Tables[0].Rows[0][3].ToString();
            cbbYGBumen.SelectedItem = ds.Tables[0].Rows[0][4].ToString();
            txtYGBirthday.Text = ds.Tables[0].Rows[0][5].ToString();
            txtYGminzu.SelectedItem = ds.Tables[0].Rows[0][6].ToString();
            cbbYGHunyin.SelectedItem = ds.Tables[0].Rows[0][7].ToString();
            txtYGZhiwu.Text = ds.Tables[0].Rows[0][8].ToString();
            txtYGPhone.Text = ds.Tables[0].Rows[0][9].ToString();
            txtYGJiuzhi.Text = ds.Tables[0].Rows[0][10].ToString();
            txtYGPay.Text = ds.Tables[0].Rows[0][12].ToString();
            operate.Get_Image(YGID, pictureBox1);


        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtYGBirthday.Text.Trim() == "" || txtYGJiuzhi.Text.Trim() == "" ||
                 txtYGminzu.Text.Trim() == "" || txtYGName.Text.Trim() == "" ||
                 txtYGNum.Text.Trim() == "" || txtYGPay.Text.Trim() == "" ||
                 txtYGPhone.Text.Trim() == "" || txtYGZhiwu.Text.Trim() == "")
                {
                    MessageBox.Show("请将信息填写完整", "警告", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    if (txtYGPhone.Text.Length != 11)
                    {
                        MessageBox.Show("手机号码为11位");
                        return;
                    }
                    else
                    {
                        string strUpdateSql = "update  tb_employee set employeeName='" + txtYGName.Text.Trim() + "',employeeSex='" + cbbYGSex.Text + "',employeeDept='" + cbbYGBumen.Text + "',employeeBirthday='" + txtYGBirthday.Text + "',employeeNation='" + txtYGminzu.Text.Trim() + "',employeeMarriage='" + cbbYGHunyin.Text + "',employeeDuty='" + txtYGZhiwu.Text.Trim() + "',employeePhone='" + txtYGPhone.Text.Trim() + "',employeeAccession='" + txtYGJiuzhi.Text + "',employeePay='" + txtYGPay.Text.Trim() + "',employeeID='" + YGID + "'";
                        int num = operate.OperateData(strUpdateSql);
                        if (OpenFileDialog1.FileName == "openFileDialog1")
                        { }
                        else
                        {
                            operate.SaveImage(this.txtYGNum.Text.Trim(), OpenFileDialog1);
                        }
                        if (num > 0)
                        {
                            string update1 = "update tb_redeploy set UName='" + txtYGName.Text + "',UOlddep='" + cbbYGBumen.Text + "',UOldJob='" + txtYGZhiwu.Text + "',UOldPay='" + txtYGPay.Text + "'where UID='" + txtYGNum.Text + "'";
                            string update2 = "update tb_prize set UName='" + txtYGName.Text + "',UserDep='" + cbbYGBumen.Text + "'where UserID='" + txtYGNum.Text + "'";
                            string update3 = "update tb_pay set YName='" + txtYGName.Text + "',YSex='" + cbbYGSex.Text + "',Ydep='" + cbbYGBumen.Text + "',YZhiwu='" + txtYGZhiwu.Text + "',YBasePay='" + txtYGPay.Text + "'where YID='" + txtYGNum.Text + "'";
                            string update4 = "update tb_chek set Pname='" + txtYGName.Text + "',Pdep='" + cbbYGBumen.Text + "'where PID='" + txtYGNum.Text + "'";
                            operate.OperateData(update1);
                            operate.OperateData(update2);
                            operate.OperateData(update3);
                            operate.OperateData(update4);
                            MessageBox.Show("员工信息修改成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            this.Close();
                        }
                    }
                }
            }
            catch (Exception EX)
            {
                MessageBox.Show(EX.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
         }

        private void button2_Click(object sender, EventArgs e)
        {
            string strg = Application.StartupPath.ToString();
            strg = strg.Substring(0,strg.LastIndexOf("\\"));
                strg = strg.Substring(0, strg.LastIndexOf("\\"));
                strg += @"\PMSImage";
                strg += @"\default.jpg";
                OpenFileDialog1.FileName=strg;
                pictureBox1.Image = System.Drawing.Image.FromFile(OpenFileDialog1.FileName);
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                string DelSql = "delete from tb_employee where employeeID='" + YGID + "'";
                operate.OperateData(DelSql);
                MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();

            }
            catch
            {
                MessageBox.Show("删除操作失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void txtYGPay_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar))
            {
                MessageBox.Show("只能输入数字", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Handled = true;
            }
        }
    }
}
